#!/system/bin/sh

# percent of disk(rest) to write
cpercent=100

# data to write path
CUR=/sdcard

#######################################################
FOLDER=data_fill_$$
mkdir -p $CUR/$FOLDER
cd $CUR/$FOLDER

# Get Disk Rest Size
st=0
strs=$(df  $CUR)
for str in ${strs[*]}
do
	if ((st++ == 10)); then
		break;
	fi
done

# case $str in 
# *G)
# 	total=${str%G*}
# 	total=$(((total * 1024)))
# 	;;
# *M)
# 	total=${str%M*}
# 	;;
# *)
# 	total=0
# 	;;
# esac
total=$((str / 1024))
if ((total < 200)); then
	echo "Too low disk size left!!!"
	exit 1
fi

# total=$(((cpercent * total / 100)))
total2=$(((total - 200)))
if ((total2 < 0)); then
	echo "Too low disk size left!!!"
	exit 1
fi
echo " data size: $total MB"
echo "Write data size: $total2 MB"
# Write Data
index=0
temp=0
while ((index++ < total2))
do
	dd if=/dev/zero of=/data/local/tmp/1m bs=8192 count=128 2> /dev/null
	ret=$(cp /data/local/tmp/1m $index 2> /dev/null)
	if ((ret != 0)); then
		echo "failed to create one file with 1MB size"
		break 1;
	fi
	percent=$(((index * 100 / total2)))
	if ((percent != temp)); then
		echo "Process $percent%"
		temp=$percent
	fi
done

exit 0
